<template>
  <div class="container">
    <div class="title">期刊内容列表</div>
    <div class="add-button">
      <el-button type="primary" @click="handleAdd">新增内容</el-button>
    </div>
    <div class="table-container">
      <el-table :data="contentList" style="width: 100%">
        <el-table-column label="序号" width="60" type="index"> </el-table-column>
        <el-table-column prop="type" label="类型">
          <template slot-scope="scope">
            {{ type[scope.row.type] }}
          </template>
        </el-table-column>
        <el-table-column prop="title" label="标题"> </el-table-column>
        <el-table-column prop="content" label="期刊介绍"> </el-table-column>
        <el-table-column prop="url" label="期刊外链">
          <template slot-scope="scope">
            <audio controls v-if="scope.row.url">
              <source :src="scope.row.url" />
            </audio>
          </template>
        </el-table-column>
        <el-table-column prop="pubdate" label="发布时间" width="120"> </el-table-column>
        <el-table-column prop="fav_nums" label="点赞数" width="120"> </el-table-column>
        <el-table-column prop="status" label="是否有效" width="120">
          <template slot-scope="scope">
            <el-tag v-if="scope.row.status === 1" type="success"> 有效 </el-tag>
            <el-tag v-else type="info"> 无效 </el-tag>
          </template>
        </el-table-column>
        <el-table-column width="200" label="操作" fixed="right">
          <template slot-scope="scope">
            <el-button
              @click="handleEdit(scope.row)"
              v-permission="{ permission: '编辑内容', type: 'disabled' }"
              class="editBtn"
              >编辑</el-button
            >
            <el-popconfirm
              confirm-button-text="确认"
              cancel-button-text="取消"
              icon="el-icon-info"
              icon-color="red"
              title="这是一段内容确定删除吗？"
              @onConfirm="handleDelete(scope.row)"
              class="deletePop"
            >
              <el-button type="danger" slot="reference">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
    </div>
    <!-- 编辑弹框 -->
    <el-dialog
      :title="dialogTitle"
      :visible.sync="showDialog"
      width="800px"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      :before-close="onCancel"
    >
      <el-form ref="form" :model="temp" :rules="rules" label-width="98px">
        <el-form-item label="内容封面" prop="image">
          <upload-imgs ref="uploadEle" :value="contentImgData" :max-num="1" />
        </el-form-item>
        <el-form-item label="内容类型" prop="type">
          <el-radio v-model="temp.type" :label="100" :disabled="dialogTitle === '编辑内容'">电影</el-radio>
          <el-radio v-model="temp.type" :label="200" :disabled="dialogTitle === '编辑内容'">音乐</el-radio>
          <el-radio v-model="temp.type" :label="300" :disabled="dialogTitle === '编辑内容'">句子</el-radio>
        </el-form-item>
        <el-form-item label="内容标题" prop="title">
          <el-col :span="11">
            <el-input v-model="temp.title" placeholder="请输入"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="内容介绍" prop="content">
          <el-col :span="11">
            <el-input type="textarea" v-model="temp.content" placeholder="请输入"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="音乐外链" prop="url">
          <el-col :span="11">
            <el-input v-model="temp.url" placeholder="请输入"></el-input>
          </el-col>
        </el-form-item>
        <el-form-item label="发布时间" prop="pubdate">
          <el-col :span="11">
            <el-date-picker v-model="temp.pubdate" type="date" placeholder="请选择"></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="有效状态">
          <el-switch v-model="temp.status" :active-value="1" :inactive-value="0"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer">
        <el-button @click="onCancel()">取 消</el-button>
        <el-button type="primary" @click="confirmSave()">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import ContentModel from '../../model/content'
import UploadImgs from '../../component/base/upload-image'
import Config from '../../config'

export default {
  name: 'list',
  components: {
    UploadImgs,
  },
  data() {
    return {
      contentList: [],
      type: {
        100: '电影',
        200: '音乐',
        300: '句子',
      },
      showDialog: false,
      temp: {
        id: null,
        image: null,
        type: null,
        title: null,
        content: null,
        url: null,
        pubdate: '',
        status: '',
      },
      delVisible: false,
      dialogTitle: '',
      contentImgData: [],
      rules: {
        image: [{ required: true, message: '内容封面不能为空', trigger: 'blur' }],
        type: [{ required: true, message: '请指定内容类型', trigger: 'blur' }],
        title: [{ required: true, message: '内容标题不能为空', trigger: 'blur' }],
        content: [{ required: true, message: '内容介绍不能为空', trigger: 'blur' }],
        url: [{ type: 'url', message: 'url格式', trigger: 'blur' }],
        pubdate: [{ required: true, message: '发布时间不能为空', trigger: 'blur' }],
      },
    }
  },
  created() {
    this.getContentList()
  },
  methods: {
    async getContentList() {
      this.contentList = await ContentModel.getContentList()
    },
    handleAdd() {
      this.dialogTitle = '新增内容'
      this.showDialog = true
    },
    handleEdit(row) {
      console.log(row)
      this.dialogTitle = '编辑内容'
      this.showDialog = true
      this.temp = {
        id: row.id,
        image: row.image,
        type: row.type,
        title: row.title,
        content: row.content,
        url: row.url,
        pubdate: row.pubdate,
        status: row.status,
      }
      this.contentImgData.push({
        display: row.image,
      })
    },
    async handleDelete(row) {
      console.log(row)
      const res = await ContentModel.delContent(row)
      this.$message.success(res.message)
      await this.getContentList()
    },
    async confirmSave() {
      const images = await this.$refs.uploadEle.getValue()
      if (this.dialogTitle === '新增内容') {
        delete this.temp.id
        this.temp.image = images.length < 1 ? '' : images[0].src
      }
      if (this.dialogTitle === '编辑内容') {
        this.temp.image = images.length < 1 ? '' : images[0].display.split(Config.baseAssetURL)[1]
      }
      console.log(this.temp)
      this.$refs.form.validate(async valid => {
        if (valid) {
          if (this.dialogTitle === '新增内容') {
            const res = await ContentModel.addContent(this.temp)
            this.$message.success(res.message)
          }
          if (this.dialogTitle === '编辑内容') {
            const res = await ContentModel.editContent(this.temp)
            this.$message.success(res.message)
          }
          this.onCancel()
          await this.getContentList()
        }
      })
    },
    onCancel() {
      this.showDialog = false
      this.resetForm()
    },
    resetForm() {
      this.contentImgData = []
      this.$refs.form.resetFields()
    },
  },
}
</script>

<style scoped lang="scss">
.container {
  .title {
    height: 59px;
    line-height: 59px;
    color: $parent-title-color;
    font-size: 20px;
    font-weight: 500;
    text-indent: 40px;
    border-bottom: 1px solid #dae1ec;
  }
  .add-button {
    padding: 20px 40px;
  }
  .table-container {
    padding: 0 40px;
  }
  .editBtn {
    margin-right: 8px;
  }
}
.deletePop {
  width: 100%;
  .el-popconfirm__main {
    margin-bottom: 8px;
  }
}
</style>
